-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

-------------------------------------------------------------------------------
-- [ MANAGMENT OF DIALOG ] --
-------------------------------------------------------------------------------

function storyboard_writeMessageAnswerForOtherPlayer(message)
if(STORYBOARD_SAVED.INFO_ANSWER_MODE) then
storyboard_writeMessageForOtherPlayer(message)
end
end

function storyboard_writeMessageObjectForOtherPlayer(message)
if(STORYBOARD_SAVED.INFO_OBJECT_MODE) then
storyboard_writeMessageForOtherPlayer(message)
end
end

--[ Ecrit un message sur le canal emote ]--
function storyboard_writeMessageForOtherPlayer(message)
     local maxLenght = 200;
     local messageTrunc = message;
     local messagePrint = "";
     local messageIsAnalysed = false;
     
     messageTrunc = string.gsub (messageTrunc, "\n", " ");
     
     while ( not messageIsAnalysed ) do
     
     if ( string.len(messageTrunc) < maxLenght ) then
        messagePrint = messageTrunc;
        SendChatMessage("[SB] " .. messagePrint, "EMOTE");
        messageIsAnalysed = true;
     else
        local limits = 0;
        local endLimits;
        
        while true do
   
           local tempLimits
           tempLimits, endLimits = string.find(messageTrunc, " ", limits + 1);
           
           if ( tempLimits == nil ) then break end
           
           if ( tempLimits < maxLenght) then
        limits = tempLimits;
           else
        break;
           end
        end
        
        messagePrint = string.sub(messageTrunc, 0, limits);
        SendChatMessage("[SB] " .. messagePrint, "EMOTE");
        messageTrunc = string.sub(messageTrunc, limits+1);
     end
      end
end


-- [ Fonction de gestion des r�ponses par d�faut ] --
function storyboard_defaultMessage(message,interactionType)
   local i = 1 ;
   while true do
   if interactionType[i] == nil then break end
      i = i + 1 ;
   end
   local idLocMax = i-1 ;


   for idLoc = 1, idLocMax, 1 do

   local keywords = interactionType[idLoc].keywords ;
   local comments = interactionType[idLoc].comments ;

      -- si j'ai les bonnes paroles ou si la r�ponse doit toujours �tre donn�e
      if storyboard_areMatchBetweenYourAnswerAndYourWords(message,keywords) or (keywords[1] == nil) then
         -- on affiche un des message par d�faut
         i = 1 ;
         while true do
            if comments[i] == nil then break end
            i = i + 1 ;
         end
         return comments[math.random(i-1)]
      end
   end


end



--[ Permet de gerer correctement le Scrolling ]--
function SbQuestFrameScrollFrame_OnUpdate()
   if STORYBOARD_NPCSAY > 0 then
      SbQuestFrameScrollFrame:SetVerticalScroll(SbQuestFrameScrollFrame:GetVerticalScrollRange())
      SbQuestFrameScrollFrameScrollBar:SetValue(SbQuestFrameScrollFrame:GetVerticalScrollRange())
      SbQuestFrameScrollFrame:UpdateScrollChildRect()
      STORYBOARD_NPCSAY = STORYBOARD_NPCSAY + 1
      if STORYBOARD_NPCSAY == 3 then STORYBOARD_NPCSAY = 0 end
   end
end

--[ Permet d'ecrire un message d'aide ]--
function storyboard.Tooltip( anchor, message, title )
        GameTooltip:SetOwner( anchor, "ANCHOR_CENTER" )
        if title then
            GameTooltip:AddLine( string.format( "|cffffffff%s|r", title ) )
        end
        GameTooltip:AddLine( string.wrap( message, 30 ) )
    GameTooltip:Show()
end

-- wraps specified text to specified line width
function string.wrap( text, limit )
    local wrapped, position, length, last, char = "", 1, string.len( text ), nil, nil
    while position < length do
        if length - position > limit then
            last = position + limit
            char = string.sub( text, last, last )
            while char ~= " " do
                last = last - 1
                char = string.sub( text, last, last )
            end
            wrapped = wrapped .. string.sub( text, position, last ) .. "\n"
            position = last + 1
        else
            wrapped = wrapped .. string.sub( text, position, -1 )
            break
        end
    end
    return wrapped
end

--[ Permet d'ecrire un message d'erreur ]--
function storyboard_debugMessage(mes)
  if STORYBOARD_DEBUG == 1 then
    DEFAULT_CHAT_FRAME:AddMessage(mes);
  end
end

--[ Permet d'envoyer un message � la boite de dialogue ]--
function storyboard_sendTextRed(msg)
   if msg ~= nil then
   SBMessageManagerFrame:SetText(SBMessageManagerFrame:GetText().."|cffff0000"..msg.."|r\n\n")
   end
end


--[ Permet d'envoyer un message � la boite de dialogue ]--
function storyboard_sendTextGreen(msg)
   if msg ~= nil then
   SBMessageManagerFrame:SetText(SBMessageManagerFrame:GetText().."|cff0ff000"..msg.."|r\n\n")
   end
end


--[ Permet d'envoyer un message � la boite de dialogue ]--
function storyboard_sendTextYellow(msg)
   if msg ~= nil then
   SBMessageManagerFrame:SetText(SBMessageManagerFrame:GetText().."|cfffccc00"..msg.."|r\n\n")
   end
end


--[ Permet d'envoyer un message � la boite de dialogue ]--
function storyboard_sendText(msg)
   if msg ~= nil then
   SBMessageManagerFrame:SetText(SBMessageManagerFrame:GetText().."|cffffffff"..msg.."|r\n\n")
   end
end


--[ redirige le bon dialogue ne fonction du lieu level = 1 chat, level = 2 ecran rs, level = 3 aide]--
function storyboard_sendAlertMessage(mes, level)
   storyboard_debugMessage("storyboard_sendAlertMessage")
   if level == 1 then
   UIErrorsFrame:AddMessage(mes, 1.0, 1.0, 1.0, 1.0, 1.0);
   end
   if level == 2 then
   UIErrorsFrame:AddMessage(mes, 1.0, 1.0, 1.0, 1.0, 1.0);
   end
   if level == 3 then
   UIErrorsFrame:AddMessage(mes, 1.0, 1.0, 1.0, 1.0, 1.0);
   end
end


-- [ Function pour ecrire dans les fenetres xml ] --
function storyboard_updateScenarioIntro()
   SbIntroHomeTitre:SetText(STORYBOARD_SCENARIO.TITLE); 
   SbIntroHomeAuteur:SetText(STORYBOARD_MES_INTERFACE_009 .. STORYBOARD_SCENARIO.AUTHOR);
   SbIntroText:SetText(STORYBOARD_SCENARIO.SCENARIO);
end
